//
//  MTDiscoverTableFooterView.swift
//  MT
//
//  Created by ooops on 2020/5/14.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

enum MTDiscoverTableFooterViewType {
    /// 势力榜
    case strength
    /// 魅力榜
    case charm
}

protocol MTDiscoverTableFooterViewDelegate: NSObjectProtocol {
    func didTap(discover: MTDiscoverTableFooterView, type: MTDiscoverTableFooterViewType)
}

class MTDiscoverTableFooterView: UIView {
    
    weak var delegate: MTDiscoverTableFooterViewDelegate?
    
    var model: MTRankingModel? {
        didSet {
            if let result = model?.result, result.count > 0 {
                if let first = result.first {
                    if first.rankingType == 1 {
                        charmImageView.mtSetImage(first.headerUrl, nil)
                    } else {
                        strengthImageView.mtSetImage(first.headerUrl, nil)
                    }
                }
                
                if let last = result.last {
                    if last.rankingType == 1 {
                        charmImageView.mtSetImage(last.headerUrl, nil)
                    } else {
                        strengthImageView.mtSetImage(last.headerUrl, nil)
                    }
                }
            }
        }
    }
    
    lazy var strengthImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 8
//        imageV.layer.borderColor = UIColor.red.cgColor
//        imageV.layer.borderWidth = 1
        imageV.image = UIImage(named: defaultPlaceholderIcon60x60)
//        imageV.tag = 10000
        imageV.layer.masksToBounds = true
        imageV.isUserInteractionEnabled = true
        imageV.contentMode = .scaleAspectFill
        return imageV
    }()
    
    lazy var strengthLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor(hex: 0xffc000)
        l.text = "实力榜"
        l.textAlignment = .center
        l.backgroundColor = UIColor.text.x1d1a1a
        let corner = UIRectCorner(rawValue: UIRectCorner.topRight.rawValue | UIRectCorner.bottomRight.rawValue)
        let maskPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 60, height: 28), byRoundingCorners: corner, cornerRadii: CGSize(width: 14, height: 14))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = CGRect(x: 0, y: 0, width: 60, height: 28)
        maskLayer.path = maskPath.cgPath
        l.layer.mask = maskLayer
        return l
    }()
    
    lazy var charmthLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor(hex: 0xffc000)
        l.text = "魅力榜"
        l.textAlignment = .center
        l.backgroundColor = UIColor.text.x1d1a1a
        let corner = UIRectCorner(rawValue: UIRectCorner.topRight.rawValue | UIRectCorner.bottomRight.rawValue)
        let maskPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 60, height: 28), byRoundingCorners: corner, cornerRadii: CGSize(width: 14, height: 14))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = CGRect(x: 0, y: 0, width: 60, height: 28)
        maskLayer.path = maskPath.cgPath
        l.layer.mask = maskLayer
        return l
    }()
    
    lazy var charmImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 8
//        imageV.layer.borderColor = UIColor.red.cgColor
//        imageV.layer.borderWidth = 1
        imageV.layer.masksToBounds = true
        imageV.image = UIImage(named: defaultPlaceholderIcon60x60)
        imageV.isUserInteractionEnabled = true
        imageV.contentMode = .scaleAspectFill
//        imageV.tag = 10001
        return imageV
    }()
    
    var imageMargin: CGFloat {
        get {
            return 6
        }
    }
    
    var lrImageMargin: CGFloat {
        get {
            return 8
        }
    }
    
    var imageWH: CGFloat {
        get {
            return (ScreenWidth - imageMargin - lrImageMargin * 2) / 2
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTap(tap:)))
        let tap1: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTap(tap:)))
        strengthImageView.addGestureRecognizer(tap1)
        charmImageView.addGestureRecognizer(tap)
        backgroundColor = UIColor(hex: 0xF4F4F4)
    }
    
    private func setupUI() {
        addSubview(strengthImageView)
        strengthImageView.addSubview(strengthLabel)
        addSubview(charmImageView)
        charmImageView.addSubview(charmthLabel)
    }
    
    private func makeConstraint() {
        strengthImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(lrImageMargin)
            make.top.equalToSuperview().offset(10)
            make.width.height.equalTo(imageWH)
        }
        
        strengthLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview()
            make.width.equalTo(60)
            make.height.equalTo(28)
        }
        
        charmthLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview()
            make.width.equalTo(60)
            make.height.equalTo(28)
        }
        
        charmImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-lrImageMargin)
            make.top.equalToSuperview().offset(10)
            make.width.height.equalTo(imageWH)
        }
    }
    
    @objc private func didTap(tap: UITapGestureRecognizer) {
        
        if tap.view == strengthImageView {
            self.delegate?.didTap(discover: self, type: .strength)
        } else {
            self.delegate?.didTap(discover: self, type: .charm)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }


}
